package in.waffl.fairycake;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class Ptolemy extends Character {
	private final Canvas canvas;
	public Ptolemy(int x, int y, final Canvas canvas) {
		super(x, y, 39, 20);
		this.setImageIndex(4);
		this.setDirection(Direction.RIGHT);
		try {
			setSprite(new Sprite(Ptolemy.class.getResourceAsStream("/in/waffl/fairycake/data/Ptolemy.png"), 39, 48, Color.magenta));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.drawable = new Drawable() {
			@Override
			public void draw(Graphics2D g) {
				getSprite().draw(g, canvas, getX(), getY()-29, getImageIndex());
			}
		};
		drawable.depth = -2;
		canvas.addDrawable(drawable);
		this.canvas = canvas;
	}

	@Override
	public void characterPoll(PollingThread p) {
		final int animationSpeed = 8;
		Rectangle collisionRect = new Rectangle(getX() + 1, getY() + getHeight(), getWidth() - 2, 1 + Math.round(getVspeed()));
		Block nextCollision = Block.collisionRectangleTop(collisionRect);
		int spell = 0;
		if (p.keyDown(KeyEvent.VK_SPACE) || isPushing()) spell = 16;
		if (getDirection() == Direction.RIGHT) {
			setImageIndex(spell + 4);
		} else {
			setImageIndex(spell + 1);
		}
		//reset walking animation when not walking
		if (getImageIndex() == spell + 3 || getImageIndex() == spell + 5) setImageIndex(spell + 4);
		if (getImageIndex() == spell + 0 || getImageIndex() == spell + 2) setImageIndex(spell + 1);
		if (p.keyDown(KeyEvent.VK_RIGHT) && nextCollision != null) {
			setImageIndex(spell + (walkAnim/animationSpeed)%3+3);
			setDirection(Direction.RIGHT);
			Block collisionRight = getCollisionRight();
			if (collisionRight == null || !collisionRight.isEdge()) setX(getX() + getWalkSpeed() + 1);
			walkAnim += 1;
		}
		if (p.keyDown(KeyEvent.VK_LEFT) && nextCollision != null) {
			setImageIndex(spell + (walkAnim/animationSpeed)%3);
			setDirection(Direction.LEFT);
			Block collisionLeft = getCollisionLeft();
			if (collisionLeft == null || !collisionLeft.isEdge()) setX(getX() - getWalkSpeed() - 1);
			walkAnim += 1;
		}
		if (p.keyDownOnce(KeyEvent.VK_UP) && nextCollision != null) {
			if (p.keyDown(KeyEvent.VK_LEFT)) setHspeed(0f - getWalkSpeed());
			if (p.keyDown(KeyEvent.VK_RIGHT)) setHspeed(getWalkSpeed());
			setVspeed(jumpSpeed);
		}
		if (p.keyDownOnce(KeyEvent.VK_N)) {
			Main.getLogicExecutor().nextLevel();
		}
		if (p.keyDownOnce(KeyEvent.VK_SPACE)) {
			boolean d = getDirection() == Direction.LEFT;
			new Spark(getX() + (d ? 0 : getWidth()), getY() + getHeight(), d ? -4 : 4, canvas);
		}
		Monster collisionMonster = Monster.collisionRectangle(new Rectangle(getX(), getY(), getWidth(), getHeight()));
		if (collisionMonster != null) die();
	}

	protected void onDie() {
		new Angel(this, 22, canvas) {
			protected void onDestroy() {
				Main.getLogicExecutor().restartLevel();
			}
		};
	}
}
